package showBooks.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import showBooks.service.ConnectionTest;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import showBooks.service.Information;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

@WebServlet(name = "ExhibitServlet",value = "/ExhibitServlet")
public class ExhibitServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8);
        PrintWriter out=resp.getWriter();

        StringBuilder str = new StringBuilder();
        int respInt = inputStreamReader.read();
        while (respInt!=-1){
            str.append((char) respInt);
            respInt = inputStreamReader.read();
        }

        //将json数据转化为自定义的Java自定义对象
        String jsonString =String.valueOf(str);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        Information information = JSONObject.toJavaObject(jsonObject,Information.class);

//
        int  id = information.getId();
        String bookName = information.getBookName();
        String userName = information.getAuthor();
        String borrowDate = information.getKind();
        String backDate = information.getCurrent_number();
        String  Email = information.getAll_number();
     //   String sta = Information.UpdateStatus(choice);

        out.write(jsonString);
        out.write(bookName);





        Connection connection = ConnectionTest.getConnection();
        Statement statement = null;
        String sql = "select * from books;";
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }











    }

}
